package com.example.springvalidatesample1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping
@Validated
public class CarController {
    @Autowired
    CarRepository repository;
    @RequestMapping(value = {"addCar1"}, method = {RequestMethod.POST})
    public ResponseResult  addCar1(@RequestBody Car car) {
        repository.save(car);
        return new ResponseResult("添加成功");
    }
//    @RequestMapping(value = {"addCar1"}, method = {RequestMethod.POST})
//    public String  addCar1(@RequestBody Car car, BindingResult bindingResult) {
////    public String addCar1(@RequestBody @Valid Car car) {
//        if (bindingResult.hasErrors()) {
//            String defaultMessage = bindingResult.getFieldErrors().get(0).getDefaultMessage();
//            return  "添加失败:$defaultMessage";
//        }
//        repository.save(car);
//        return "添加成功";
//    }
}
